package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsMchntOrderInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsMchntOrderInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 商户订单表操作实现
 * @Author panky
 * @date 2020/5/18
 **/
public class CopsMchntOrderInfoMapperImpl implements CopsMchntOrderInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoByInstIdMchntNoMchntOrderId(String instId, String mchntNo, String mchntOrderId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectMchntOrderInfoByInstIdAndMchntOrderId");

        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setMchntNo(mchntNo);
        mchntOrderInfo.setMchntOrderId(mchntOrderId);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        return objects.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoByInstIdMchntNo(CopsMchntOrderInfo mchntOrderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectMchntOrderInfoByInstIdMchntNo");

        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getBeginDate())) {
            sqlStr = sqlStr + " AND TXN_DATE >= " + mchntOrderInfo.getBeginDate();
        }
        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getEndDate())) {
            sqlStr = sqlStr + " AND TXN_DATE <= " + mchntOrderInfo.getEndDate();
        }
        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getMinAmt())) {
            sqlStr = sqlStr + " AND ORDER_AMT >= " + AmountUtil.changeF2Y(mchntOrderInfo.getMinAmt());
        }
        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getMaxAmt())) {
            sqlStr = sqlStr + " AND ORDER_AMT <= " + AmountUtil.changeF2Y(mchntOrderInfo.getMaxAmt());
        }
        rglog.debug("执行sql语句:{} ", sqlStr);
        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        return objects.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoSysSeqNum(String instId, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectMchntOrderInfoSysSeqNum");

        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setSysSeqNum(sysSeqNum);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        return objects.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public CopsMchntOrderInfo selectCopsmchntOrderInfoByOrigSysSeqNumAndMchntRefundId(String instId, String mchntRefundId, String origSysSeqNum, String mchntNo) throws Exception {
        CopsMchntOrderInfo copsMchntOrderInfo = new CopsMchntOrderInfo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntOrderInfoByOrigSysSeqNumAndMchntRefundId");

        copsMchntOrderInfo.setInstId(instId);
        copsMchntOrderInfo.setMchntOrderId(mchntRefundId);
        copsMchntOrderInfo.setOrigSysSeqNum(origSysSeqNum);
        copsMchntOrderInfo.setMchntNo(mchntNo);
        rglog.debug("查询商户订单退款信息<{}>", JSON.toJSONString(copsMchntOrderInfo));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, copsMchntOrderInfo, copsMchntOrderInfo);
        rglog.debug("查询商户订单退款信息结果为<{}>", iReturnCode);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("查询商户订单退款信息失败");
            return null;
        }
        return copsMchntOrderInfo;
    }
}
